﻿using Microsoft.Ajax.Utilities;
using System.Web.Mvc;

namespace Raffles.OA.WebApp.App_Filter
{
    /// <summary>
    /// 全局过滤器-登录
    /// </summary>
    public class LoginAttribute : ActionFilterAttribute
    {
        public bool Ignore = true;
        public LoginAttribute(bool ignore = true)
        {
            Ignore = ignore;
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            if (Ignore == false)
            {
                return;
            }
            string returnUrl = filterContext.HttpContext.Request.Url.ToString();
            string redirectUrl = string.Format("/Login/Index?returnUrl={0}", returnUrl);
            if (OperatorProvider.Provider.GetCurrent() == null)
            {
                filterContext.HttpContext.Response.Redirect(redirectUrl, true);
                filterContext.Result = new EmptyResult();
                return;
            }
        }
    }
}